"use strict";
document.addEventListener("DOMContentLoaded", () => {
  //import { createApp } from "vue";
  const { createApp } = Vue;

  let id = 0;
  class shift {
    constructor(id, name) {
      this.id = id;
      this.shift = name;
      this.is_shift_constraints = true;
      this.shift_constraints = {
        hard_min:'',
        soft_min:'',
        hard_max:'',
        soft_max:'',
      };
      this.is_weekly_sum_constraints = true;
      this.weekly_sum_constraints = {
        hard_min:'',
        soft_min:'',
        hard_max:'',
        soft_max:'',
      };
      this.is_days_sum_constraints = true;
      this.days_sum_constraints = {
        days:'',
        hard_min:'',
        soft_min:'',
        hard_max:'',
        soft_max:'',
      };
      this.weekly_cover_demands = [];
    }
  }
  createApp({
    data() {
      return {
        num_employees: 0,
        start_date: "",
        end_date: "",
        shifts: [],
        weekdays: ["周一", "周二", "周三", "周四", "周五", "周六", "周天"],
        msg: { isMsg: false, msg: "", title: "" },
      };
    },
    computed: {
      num_days() {
        return (
          (Date.parse(this.end_date) - Date.parse(this.start_date)) / 86400000 +
          1
        );
      },
      //格式化为后端需要的数据数据
      formatDate() {
        var data = {
          shifts: [],
          days_sum_constraints: [],
          weekly_sum_constraints: [],
          weekly_cover_demands: [],
          excess_cover_penalties: [],
          shift_constraints: [],
          fixed_assignments: [],
          requests: [],
          penalized_transitions: [],
          weekly_cover_demands_max: [],
        };
        data.num_days = this.num_days;
        data.start_date = this.start_date;
        data.end_date = this.end_date;
        data.num_employees = this.num_employees;
        data.num_weeks = Math.floor(data.num_days / 7);
        for (let i = 0; i < this.shifts.length; i++) {
          const shift = this.shifts[i];
          data.shifts.push(shift.shift);
          if (shift.is_days_sum_constraints) {
            data.days_sum_constraints.push([
              shift.days_sum_constraints.days,
              i,
              shift.days_sum_constraints.hard_min,
              shift.days_sum_constraints.soft_min,
              5,
              shift.days_sum_constraints.soft_max,
              shift.days_sum_constraints.hard_max,
              5,
            ]);
          }
          if (shift.is_weekly_sum_constraints) {
            data.weekly_sum_constraints.push([
              i,
              shift.weekly_sum_constraints.hard_min,
              shift.weekly_sum_constraints.soft_min,
              5,
              shift.weekly_sum_constraints.soft_max,
              shift.weekly_sum_constraints.hard_max,
              5,
            ]);
          }
          if (shift.is_shift_constraints) {
            data.shift_constraints.push([
              i,
              shift.shift_constraints.hard_min,
              shift.shift_constraints.soft_min,
              5,
              shift.shift_constraints.soft_max,
              shift.shift_constraints.hard_max,
              5,
            ]);
          }
        }
        for (let i = 0; i < 7; i++) {
          data.weekly_cover_demands.push([]);
          for (let j = 0; j < this.shifts.length; j++) {
            data.weekly_cover_demands[i][j] =
              this.shifts[j].weekly_cover_demands[i];
          }
        }
        for (let j = 0; j < this.shifts.length; j++) {
          data.excess_cover_penalties.push(5);
        }
        return data;
      },
    },
    methods: {
      addShift() {
        this.shifts.push({
          id: id++,
          shift_constraints: {},
          weekly_sum_constraints: {},
          days_sum_constraints: {},
          weekly_cover_demands: [],
        });
      },
      delShift(shift) {
        this.shifts = this.shifts.filter((e) => {
          return e.id != shift.id;
        });
      },
      //执行python主程序（先储存数据，然后开始排班）
      async submit() {
        const res = await axios.post("/dataFromJs", this.formatDate);
        this.showError(res);
      },
      //生成模板
      async template() {
        const res = await axios.post("/template", this.formatDate);
        this.showError(res);
      },
      //读取配置
      async loadConfig() {
        const res = await axios.get("loadConfig");
        const data = res.data;
        this.start_date = data.start_date;
        this.end_date = data.end_date;
        this.num_employees = data.num_employees;
        this.shifts = [];
        for (const e of data.shifts) {
          this.shifts.push(new shift(id++, e));
        }
        for (let i = 0; i < data.shifts.length; i++) {
          let shift = this.shifts[i];
          let temp = data.days_sum_constraints.filter((e) => {
            return e[1] == i;
          });
          if (temp.length > 0) {
            let days_sum_constraints = temp[0];
            shift.is_days_sum_constraints = true;
            shift.days_sum_constraints.days = days_sum_constraints[0];
            shift.days_sum_constraints.hard_min = days_sum_constraints[2];
            shift.days_sum_constraints.soft_min = days_sum_constraints[3];
            shift.days_sum_constraints.soft_max = days_sum_constraints[5];
            shift.days_sum_constraints.hard_max = days_sum_constraints[6];
          } else {
            shift.is_days_sum_constraints = false;
          }
          temp = data.weekly_sum_constraints.filter((e) => {
            return e[0] == i;
          });
          if (temp.length > 0) {
            let weekly_sum_constraints = temp[0];
            shift.is_weekly_sum_constraints = true;
            shift.weekly_sum_constraints.hard_min = weekly_sum_constraints[1];
            shift.weekly_sum_constraints.soft_min = weekly_sum_constraints[2];
            shift.weekly_sum_constraints.soft_max = weekly_sum_constraints[4];
            shift.weekly_sum_constraints.hard_max = weekly_sum_constraints[5];
          } else {
            shift.is_weekly_sum_constraints = false;
          }
          temp = data.shift_constraints.filter((e) => {
            return e[0] == i;
          });
          if (temp.length > 0) {
            let shift_constraints = temp[0];
            shift.is_shift_constraints = true;
            shift.shift_constraints.hard_min = shift_constraints[1];
            shift.shift_constraints.soft_min = shift_constraints[2];
            shift.shift_constraints.soft_max = shift_constraints[4];
            shift.shift_constraints.hard_max = shift_constraints[5];
          } else {
            shift.is_shift_constraints = false;
          }
        }

        for (let i = 0; i < 7; i++) {
          for (let j = 0; j < this.shifts.length; j++) {
            this.shifts[j].weekly_cover_demands[i] =
              data.weekly_cover_demands[i][j];
          }
        }
      },
      closeMsg() {
        this.msg.isMsg = false;
      },
      showError(res) {
        if (res.status == 200) {
          this.msg.title ="成功"
        }else if(res.status == 500){
          this.msg.title ="程序内部错误"
        }else{
          this.msg.title ="未知错误"
        }
        console.log(res.data)
        this.msg.msg = res.data.split(/\n/);
        this.msg.isMsg = true;
      },
    },
  }).mount("#vueApp");
});
